<?php
$sort = $this->sort;
$sort_name = array("class"=>"","rel"=>"asc");
$sort_status = array("class"=>"","rel"=>"asc");

switch($sort['by']){
    case "groups_name": 
        $sort_name = ($sort['type']=="asc")?array("class"=>" sorting_desc","rel"=>"desc"):array("class"=>" sorting_asc","rel"=>"asc");
        break;
    case "groups_status": 
       $sort_status = ($sort['type']=="asc")?array("class"=>" sorting_desc","rel"=>"desc"):array("class"=>" sorting_asc","rel"=>"asc");
        break;
}
?>
<table class="data display datatable">
    <thead>
        <tr>
            <th width="50" class="center">STT</th>
            <th class="sorting<?=$sort_name['class']?>">
                <a href="<?=$this->url(array("module"=>"admin","controller"=>"user-group","action"=>"list","sortby"=>"groups_name"))?>" rel="<?=$sort_name["rel"]?>">Tên nhóm người dùng</a>
            </th>
            <th width="70" class="sorting<?=$sort_status['class']?>">
                <a href="<?=$this->url(array("module"=>"admin","controller"=>"user-group","action"=>"list","sortby"=>"groups_status"))?>" rel="<?=$sort_status['rel']?>">Hiển thị</a>
            </th>
            <th width="50" class="center">Sửa</th>
            <th  width="50"  class="center">Xóa</th>
            <th width="70">Phân quyền</th>
        </tr>                  
    </thead>
    
    <tbody>
        <?php $stt=1; foreach($this->datas as $row){ $parity = ($stt%2)?"odd":"even";?>
        <tr class="gradeA <?=$parity?>">
            <td><?=$stt?></td>
            <td class="zend_form"><?=$row->groups_name?><span class="group-name" style="display: none;"><?=$row->groups_name?></span></td>
            <td class="center">
                <?php if($row->idgroups!=1){?>
                <a class="choice-multi" rel="<?=$row->groups_status?>" href="<?=$this->url(array("module"=>"admin","controller"=>"user-group","action"=>"list","choice"=>"multi","id"=>$row->idgroups))?>">
                    <?php $ico = ($row->groups_status)?"star_list.png":"star_unlist.png"?>
                    <img src="<?=$this->baseUrl("templates/admin/img/$ico")?>" />
                </a>
                <?php }else{?>
                <span class="error">[Not allow]</span>
                <?php }?>
            </td>
            <td class="center">
                <a class="action-edit" href="<?=$this->url(array("module"=>"admin","controller"=>"user-group","action"=>"edit","id"=>$row->idgroups), null, true)?>">
                    <img src="<?=$this->baseUrl("templates/admin/img/icon-edit.png")?>" alt="[Sửa]" title="Sửa"/>
                </a>
                <a href="<?=$this->baseUrl("admin/user-group/list/do/save/id/$row->idgroups")?>" class="action-save" style="display: none; color: blue;">
                    [Lưu]
                </a>
                <a href="#" class="action-cancel" style="display: none; color: red;">[Hủy]</a>
            </td>
            <td>
                <a class="del" href="<?=$this->url(array("module"=>"admin","controller"=>"user-group","action"=>"list","do"=>"del","id"=>$row->idgroups))?>">
                    <img src="<?=$this->baseUrl("templates/admin/img/icon-del.png")?>" alt="[Xóa]" title="Xóa"/>
                </a>
            </td>
            
            <td width="80" class="center">
                <?php if($row->idgroups!=1){?>
                <a class="action-perm" href="<?=$this->url(array("module"=>"admin","controller"=>"user-group","action"=>"permission","idgroup"=>$row->idgroups))?>">
                    <img src="<?=$this->baseUrl("templates/admin/img/group_icon_500.png")?>" alt="[Phân quyền]" title="Phân quyền"/>
                </a>
                <?php }else{?>
                    <span class="error">[Full]</span>
                <?php }?>
            </td>
        </tr>
        <?php $stt++;}?>
    </tbody>
</table>

<?php if($this->datas->count()>1){// Nhiều hơn 1 trang thì sử dụng phân trang?>
<div class="dataTables_paginate paging_full_numbers">
    <?=$this->paginationControl($this->datas,"Sliding","partial/pagination-control.phtml")?>
</div>
<?php }?>
<br />


<script>
$(function(){    
    /*Xóa*/
    $(".del").click(function(){
        var _this = $(this);
        $(".dialog-message").html("<div class='message warning'><h5>Cảnh báo</h5><p>Bạn có chắc muốn thực hiện hành động này không?</p></div>")
                            .dialog({            
            autoOpen: true,
            modal: true,
            buttons: {
                "Có": function(){                    
                    var link =  _this.attr("href");
                    
                    $.get(link,{},function(rdata){
                        if(rdata==0){
                            $(".dialog-message").html("<div class='message error'><h5>Không thể xóa!</h5><p>Vì nhóm này là nhóm mặc định.<br/>Hoặc nhóm đang tồn tại các tài khoản người dùng!</p></div>")
                            .dialog({
                                buttons:{"OK":function(){$(this).dialog("close");}}
                            })
                        }else{
                            _this.parent("td").parent("tr").remove();
                        }    
                    },"json");
                    $(this).dialog("close");
                },
                "Không": function(){
                    $(this).dialog("close");
                }
            }
        })
        return false;
    })
    
    
     /*Phân trang*/
    $(".pagination a").click(function(){
        var link = $(this).attr("href");
        $(".loading").show();
        $.get(link,null,function(rdata){
            $(".dataTables_wrapper").html(rdata);
            $(".loading").fadeOut(1000);
        })
        return false;
    })
    
    /*Sắp xếp*/
    $(".sorting a").click(function(){
        var link = $(this).attr("href");
        var type = $(this).attr("rel");
        var filter_value = $(".searchkey").val();
        var sdata = {"sorttype":type,"filter_field":"groups_name","filter_value":filter_value};
        if(filter_value.length==0){
            sdata={"sorttype":type};
        }
        $(".loading").show();
        $.post(link,sdata,function(rdata){
            $(".dataTables_wrapper").html(rdata);
            $(".loading").fadeOut(1000);
        })
        return false;
    })
    
    //Enable 0r Disable
    $(".choice-multi").click(function(){
        var icon = $(this).children("img").attr("src");
        var status = $(this).attr("rel");
        var link = $(this).attr("href");
        if(status==0){            
            var n_icon = icon.replace("star_unlist","star_list");
            $(this).attr("rel",1);
        }else{
            var n_icon = icon.replace("star_list","star_unlist");
            $(this).attr("rel",0);
        }
        $(this).children("img").attr("src",n_icon);
        $.get(link,{},function(){},"json");
        return false;
    })
    
    //edit row
    $(".action-edit").click(function(){
        $(this).next().next("a.action-cancel").show();
        $(this).hide().next("a.action-save").show();
        var group_name = $(this).parent("td").prev("td").prev("td").children("span.group-name").text();
        var html = "<label class='required'><strong>Tên nhóm: </strong></label>  <input rel='"+group_name+"' class='group-name' size='50' type='text' value='"+group_name+"' /><span class='error'></span>";
        $(this).parent("td").prev("td").prev("td").html(html);
        return false;
    });
    
    //save row
    $(".action-save").click(function(){
        var o_group_name = $(this).parent("td").prev("td").prev("td")
                                .children("input.group-name");
        var group_name = o_group_name.val();
        var link = $(this).attr("href");
        if(group_name==""){
            o_group_name.addClass("error");
            o_group_name.next("span.error").text(" Tên nhóm không được để trống.");
            
        }else{
            o_group_name.parent("td").html(group_name+"<span class='group-name' style='display:none'>"+group_name+"</span>");
            $.get(link,{"group_name":group_name},function(rdata){
                
            },"json");            
            $(this).hide();
            $(this).prev(".action-edit").show();
            $(this).next(".action-cancel").hide();
        }
        return false;
    });
    
    
    // cancel click
    $(".action-cancel").click(function(){
        var group_name = $(this).parent("td").prev("td").prev("td").children("input.group-name").attr("rel");
        var html = group_name+"<span class='group-name' style='display: none;'>"+group_name+"</span>";
        $(this).parent("td").prev("td").prev("td").html(html);
        $(this).hide();
        $(this).prev(".action-save").hide().prev(".action-edit").show();
        return false;
    });
    
})
</script>